package org.jeecg.modules.zcgl.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.service.IPublicAccountTransactionInquiryService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 查询对公账户交易明细记录
 */
@Slf4j
public class PublicAccountTransactionInquiryJob implements Job {

    @Autowired
    private ISysDepartService departService;
    @Autowired
    private IPublicAccountTransactionInquiryService inquiryService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

//        String dateStr = "20210122";
        Date date = DateUtils.nextDay(-1);
//        String dateStr = DateUtils.yyyyMMdd.get().format(date);
//        String key = "PublicAccountTransactionInquiryJob:" + dateStr;
//        if (redisUtil.get(key) != null) {
//            return;
//        }
//        redisUtil.set(key, true);
        long start = System.currentTimeMillis();
        log.info("查询对公账户交易明细记录下载开始：" + start);
        try {

            List<SysDepart> departList = departService.queryCompanyList();
            if (departList == null) {
                return;
            }

            for (SysDepart depart : departList) {
                inquiryService.publicAccountTransactionInquiryByDepart(depart, date);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询对公账户交易明细记录下载失败！");
        }


        long end = System.currentTimeMillis();
        log.info("查询对公账户交易明细记录结束：" + end);
        log.info("查询对公账户交易明细记录下载耗时：" + (end - start) / 1000 + "S");
    }

//    private void a(PublicAccountTransactionInquiryPostVO vo, SysDepart depart) throws Exception {
//        ResponseData<String> messageCallBack = MessageProcess.getResponseMessageCallBack(vo,
//                vo.getPartner(), ccqtgbPropertyConfig.getPrivateKeyPath(), ccqtgbPropertyConfig.getAesKey(),
//                null, 200, "");
//        //发送post请求
//        String resultString = HttpClientUtil.doPostJson(ccqtgbPropertyConfig.getPublicAccountTransactionInquiry(), JSON.toJSONString(messageCallBack));
//        log.info(depart.getDepartName() + "查询对公账户交易明细记录返回结果：" + resultString);
//        //返回结果解密
////                    RequestData<Map> dataReslt = ApiDataUtils.getRequestData(resultString, Map.class);
////                    System.out.println(JSON.toJSONString(dataReslt));
//        RequestData<PublicAccountTransactionInquiryResultVO> dataReslt = ApiDataUtils.getRequestData(resultString, PublicAccountTransactionInquiryResultVO.class);
//        PublicAccountTransactionInquiryResultVO resultVO = dataReslt.getBody();
//        if ("0000".equalsIgnoreCase(resultVO.getRetcode())) {
////                        StatementAccount statementAccount = new StatementAccount();
////                        BeanUtils.copyProperties(resultVO, statementAccount, "date");
////                        statementAccount.setDate(DateUtils.str2Date(resultVO.getDate(), DateUtils.yyyyMMdd.get()));
////                        statementAccount.setCompanyId(depart.getId());
////
////                        // TDD
////                        statementAccount.setRecordedCompanyName(depart.getDepartName());
////
////                        List<StatementAccountDetails> statementAccountDetailsList = this.creatorDetails(resultVO.getItems());
////                        this.saveOrUpdateMain(statementAccount, statementAccountDetailsList);
//
//            if (StringUtils.isBlank(resultVO.getPageCount())) {
//                return;
//            }
//            if (resultVO.getPageCount().compareTo(vo.getPageNo()) > 0) {
//
//            }
//        } else {
//            throw new JeecgBootException("查询对公账户交易明细记录下载失败");
//        }
//    }
}
